| Conditions | 1 |
| Paths | 1 |
| Total Lines | 204 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* global wc_stripe_apple_pay_single_params, Stripe */ |
||
| 4 | jQuery( function( $ ) { |
||
| 5 | 'use strict'; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Object to handle Stripe payment forms. |
||
| 9 | */ |
||
| 10 | var wc_stripe_apple_pay_single = { |
||
| 11 | /** |
||
| 12 | * Get WC AJAX endpoint URL. |
||
| 13 | * |
||
| 14 | * @param {String} endpoint Endpoint. |
||
| 15 | * @return {String} |
||
| 16 | */ |
||
| 17 | getAjaxURL: function( endpoint ) { |
||
| 18 | return wc_stripe_apple_pay_single_params.ajaxurl |
||
| 19 | .toString() |
||
| 20 | .replace( '%%endpoint%%', 'wc_stripe_' + endpoint ); |
||
| 21 | }, |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Initialize event handlers and UI state. |
||
| 25 | */ |
||
| 26 | init: function() { |
||
|
|
|||
| 27 | Stripe.applePay.checkAvailability( function( available ) { |
||
| 28 | if ( available ) { |
||
| 29 | $( document.body ).on( 'woocommerce_variation_has_changed', function() { |
||
| 30 | wc_stripe_apple_pay_single.generate_cart(); |
||
| 31 | }) |
||
| 32 | |||
| 33 | .on( 'change', '.quantity .qty', function() { |
||
| 34 | wc_stripe_apple_pay_single.generate_cart(); |
||
| 35 | }); |
||
| 36 | |||
| 37 | wc_stripe_apple_pay_single.generate_cart(); |
||
| 38 | |||
| 39 | $( '.apple-pay-button' ).show(); |
||
| 40 | } |
||
| 41 | }); |
||
| 42 | |||
| 43 | $( document.body ).on( 'click', '.apple-pay-button', function( e ) { |
||
| 44 | e.preventDefault(); |
||
| 45 | |||
| 46 | var addToCartButton = $( '.single_add_to_cart_button' ); |
||
| 47 | |||
| 48 | // First check if product can be added to cart. |
||
| 49 | if ( addToCartButton.is( '.disabled' ) ) { |
||
| 50 | if ( addToCartButton.is( '.wc-variation-is-unavailable' ) ) { |
||
| 51 | window.alert( wc_add_to_cart_variation_params.i18n_unavailable_text ); |
||
| 52 | } else if ( addToCartButton.is( '.wc-variation-selection-needed' ) ) { |
||
| 53 | window.alert( wc_add_to_cart_variation_params.i18n_make_a_selection_text ); |
||
| 54 | } |
||
| 55 | |||
| 56 | return; |
||
| 57 | } |
||
| 58 | |||
| 59 | var paymentRequest = { |
||
| 60 | countryCode: wc_stripe_apple_pay_single_params.country_code, |
||
| 61 | currencyCode: wc_stripe_apple_pay_single_params.currency_code, |
||
| 62 | total: { |
||
| 63 | label: wc_stripe_apple_pay_single_params.label, |
||
| 64 | amount: wc_stripe_apple_pay_single_params.total |
||
| 65 | }, |
||
| 66 | lineItems: wc_stripe_apple_pay_single_params.line_items, |
||
| 67 | requiredBillingContactFields: ['postalAddress'], |
||
| 68 | requiredShippingContactFields: 'yes' === wc_stripe_apple_pay_single_params.needs_shipping ? ['postalAddress', 'phone', 'email', 'name'] : ['phone', 'email', 'name'] |
||
| 69 | }; |
||
| 70 | |||
| 71 | var applePaySession = Stripe.applePay.buildSession( paymentRequest, function( result, completion ) { |
||
| 72 | var data = { |
||
| 73 | 'nonce': wc_stripe_apple_pay_single_params.stripe_apple_pay_nonce, |
||
| 74 | 'result': result |
||
| 75 | }; |
||
| 76 | |||
| 77 | $.ajax({ |
||
| 78 | type: 'POST', |
||
| 79 | data: data, |
||
| 80 | url: wc_stripe_apple_pay_single.getAjaxURL( 'apple_pay' ), |
||
| 81 | success: function( response ) { |
||
| 82 | if ( 'true' === response.success ) { |
||
| 83 | completion( ApplePaySession.STATUS_SUCCESS ); |
||
| 84 | window.location.href = response.redirect; |
||
| 85 | } |
||
| 86 | |||
| 87 | if ( 'false' === response.success ) { |
||
| 88 | completion( ApplePaySession.STATUS_FAILURE ); |
||
| 89 | |||
| 90 | $( '.apple-pay-button' ).before( '<p class="woocommerce-error wc-stripe-apple-pay-error">' + response.msg + '</p>' ); |
||
| 91 | |||
| 92 | // Scroll to error so user can see it. |
||
| 93 | $( document.body ).animate({ scrollTop: $( '.wc-stripe-apple-pay-error' ).offset().top }, 500 ); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | }); |
||
| 97 | }); |
||
| 98 | |||
| 99 | // If shipping is needed -- get shipping methods. |
||
| 100 | if ( 'yes' === wc_stripe_apple_pay_single_params.needs_shipping ) { |
||
| 101 | // After the shipping contact/address has been selected |
||
| 102 | applePaySession.onshippingcontactselected = function( shipping ) { |
||
| 103 | var data = { |
||
| 104 | 'nonce': wc_stripe_apple_pay_single_params.stripe_apple_pay_get_shipping_methods_nonce, |
||
| 105 | 'address': shipping.shippingContact |
||
| 106 | }; |
||
| 107 | |||
| 108 | $.ajax({ |
||
| 109 | type: 'POST', |
||
| 110 | data: data, |
||
| 111 | url: wc_stripe_apple_pay_single.getAjaxURL( 'apple_pay_get_shipping_methods' ), |
||
| 112 | success: function( response ) { |
||
| 113 | var total = { |
||
| 114 | 'label': wc_stripe_apple_pay_single_params.label, |
||
| 115 | 'amount': response.total |
||
| 116 | }; |
||
| 117 | |||
| 118 | if ( 'true' === response.success ) { |
||
| 119 | applePaySession.completeShippingContactSelection( ApplePaySession.STATUS_SUCCESS, response.shipping_methods, total, response.line_items ); |
||
| 120 | } |
||
| 121 | |||
| 122 | if ( 'false' === response.success ) { |
||
| 123 | applePaySession.completeShippingContactSelection( ApplePaySession.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS, response.shipping_methods, total, response.line_items ); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | }); |
||
| 127 | }; |
||
| 128 | |||
| 129 | // After the shipping method has been selected |
||
| 130 | applePaySession.onshippingmethodselected = function( event ) { |
||
| 131 | var data = { |
||
| 132 | 'nonce': wc_stripe_apple_pay_single_params.stripe_apple_pay_update_shipping_method_nonce, |
||
| 133 | 'selected_shipping_method': event.shippingMethod |
||
| 134 | }; |
||
| 135 | |||
| 136 | $.ajax({ |
||
| 137 | type: 'POST', |
||
| 138 | data: data, |
||
| 139 | url: wc_stripe_apple_pay_single.getAjaxURL( 'apple_pay_update_shipping_method' ), |
||
| 140 | success: function( response ) { |
||
| 141 | var newTotal = { |
||
| 142 | 'label': wc_stripe_apple_pay_single_params.label, |
||
| 143 | 'amount': parseFloat( response.total ).toFixed(2) |
||
| 144 | }; |
||
| 145 | |||
| 146 | if ( 'true' === response.success ) { |
||
| 147 | applePaySession.completeShippingMethodSelection( ApplePaySession.STATUS_SUCCESS, newTotal, response.line_items ); |
||
| 148 | } |
||
| 149 | |||
| 150 | if ( 'false' === response.success ) { |
||
| 151 | applePaySession.completeShippingMethodSelection( ApplePaySession.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS, newTotal, response.line_items ); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | }); |
||
| 155 | }; |
||
| 156 | } |
||
| 157 | |||
| 158 | applePaySession.begin(); |
||
| 159 | }); |
||
| 160 | }, |
||
| 161 | |||
| 162 | get_attributes: function() { |
||
| 163 | var select = $( '.variations_form' ).find( '.variations select' ), |
||
| 164 | data = {}, |
||
| 165 | count = 0, |
||
| 166 | chosen = 0; |
||
| 167 | |||
| 168 | select.each( function() { |
||
| 169 | var attribute_name = $( this ).data( 'attribute_name' ) || $( this ).attr( 'name' ); |
||
| 170 | var value = $( this ).val() || ''; |
||
| 171 | |||
| 172 | if ( value.length > 0 ) { |
||
| 173 | chosen ++; |
||
| 174 | } |
||
| 175 | |||
| 176 | count ++; |
||
| 177 | data[ attribute_name ] = value; |
||
| 178 | }); |
||
| 179 | |||
| 180 | return { |
||
| 181 | 'count' : count, |
||
| 182 | 'chosenCount': chosen, |
||
| 183 | 'data' : data |
||
| 184 | }; |
||
| 185 | }, |
||
| 186 | |||
| 187 | generate_cart: function() { |
||
| 188 | var data = { |
||
| 189 | 'nonce': wc_stripe_apple_pay_single_params.stripe_apple_pay_cart_nonce, |
||
| 190 | 'qty': $( '.quantity .qty' ).val(), |
||
| 191 | 'attributes': $( '.variations_form' ).length ? wc_stripe_apple_pay_single.get_attributes().data : [] |
||
| 192 | }; |
||
| 193 | |||
| 194 | return $.ajax({ |
||
| 195 | type: 'POST', |
||
| 196 | data: data, |
||
| 197 | url: wc_stripe_apple_pay_single.getAjaxURL( 'generate_apple_pay_single' ), |
||
| 198 | success: function( response ) { |
||
| 199 | wc_stripe_apple_pay_single_params.total = response.total; |
||
| 200 | wc_stripe_apple_pay_single_params.line_items = response.line_items; |
||
| 201 | } |
||
| 202 | }); |
||
| 203 | } |
||
| 204 | }; |
||
| 205 | |||
| 206 | wc_stripe_apple_pay_single.init(); |
||
| 207 | }); |
||
| 208 |